package cn.eonshine.cuba.lsms.modular.basic.service.impl;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.eonshine.cuba.inf.common.util.DateUtils;
import cn.eonshine.cuba.inf.common.http.InfStatus;
import cn.eonshine.cuba.inf.modular.basic.entity.Ecc;
import cn.eonshine.cuba.lsms.modular.CommonService;
import cn.eonshine.cuba.lsms.modular.basic.entity.Office;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeClientService;
import cn.eonshine.cuba.lsms.modular.basic.service.IOfficeService;

@Service
public class OfficeService extends CommonService<Ecc> implements IOfficeClientService {
	
	@Autowired
	private IOfficeService officeService;
	
	private final String listUrl = "/ecc/list";
	private final String updateUrl = "/ecc/update";
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.eonshine.cuba.inf.modular.lsms.basic.service.IInventoryDataService#
	 * requestInventoryData()
	 */
	@Override
	public void requestOfficeInf() {
		requestData(listUrl, updateUrl, InfStatus.INF_LOG_TYPE.ECC_NUM);
	}
	
	
	@Override
	protected Object dataHandle(Ecc ecc) throws Exception {
		QueryWrapper<Office> entity = new QueryWrapper<Office>();
		Office info = null;
		if(StringUtils.isNotBlank(ecc.getId())) {
			entity.eq("CODE", ecc.getCode());
			entity.eq("TYPE", InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_ECC);
			info = officeService.getOne(entity);
		}else {
			info = officeService.getById(ecc.getId());
		}

		boolean isInsert = false;
		if (info == null) {
			info = new Office();
			isInsert = true;
		}
		info.setType(InfStatus.LSMS_OFFICE_TYPE.ORG_TYPE_ECC);
		info.setCode(ecc.getCode());
		info.setName(StringUtils.trim(ecc.getName()));
		info.setFullName(StringUtils.trim(ecc.getFullName()));
		info.setUseable(ecc.getUseable());
		info.setIsFromInf(InfStatus.IS_FROM_INTERFACE);
		if(isInsert) {
			info.setAddress(ecc.getAddress());
			info.setZipCode(ecc.getZipCode());
			info.setMaster(ecc.getMaster());
			info.setPhone(ecc.getPhone());
			info.setFax(ecc.getFax());
			info.setEmail(ecc.getEmail());
			info.setLongitude(ecc.getLongitude());
			info.setLatitude(ecc.getLatitude());
			info.setSubDepotCode(ecc.getSubDepotCode());
			info.setProvince(ecc.getProvince());
			info.setCreateBy(InfStatus.CREATE_BY);
			info.setParentId(InfStatus.LSMS_OFFICE_PARENT.SYS_UNION_CUPET);
			info.setParentIds(InfStatus.LSMS_OFFICE_PARENT.ROOT+InfStatus.SEPARATOR_CHAR+InfStatus.LSMS_OFFICE_PARENT.SYS_UNION_CUPET+InfStatus.SEPARATOR_CHAR);
			if(ecc.getCreateDate() == null) {
				info.setCreateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
			}else {
				info.setCreateDate(ecc.getCreateDate());
			}
		}
		
		info.setUpdateBy(InfStatus.UPDATE_BY);
		info.setUpdateDate(DateUtils.getCubaDate(Calendar.getInstance().getTime()));
		
		if (isInsert) {
			officeService.save(info);
		} else {
			officeService.updateById(info);
		}
		return info;
	}
	
	@Override
	protected List<Ecc> feadBackDataHandle(Object obj, Ecc ecc, List<Ecc> feadBackDataLists) throws Exception {
		Office info = (Office) obj;
		// 返回进行更新的数据
		Ecc tmpBack = new Ecc();
		tmpBack.setCode(info.getCode());
		tmpBack.setDataInputDatetime(ecc.getDataInputDatetime());
		tmpBack.setLsmsUpdate(InfStatus.LSMS_UPDATE_EXTRACT_YES);
		tmpBack.setId(info.getId());
		feadBackDataLists.add(tmpBack);
		return feadBackDataLists;
	}
}
